package com.yingjie.admin_backend.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yingjie.admin_backend.model.Room;
import com.yingjie.admin_backend.repository.RoomRepository;

@Service
public class RoomService {

    @Autowired
    private RoomRepository roomRepository;

    private static final Logger logger = LoggerFactory.getLogger(RoomService.class);

    public void createRoom(Room room) {
        logger.info("Creating room title={} price={} location={}", room.getTitle(), room.getPrice(), room.getLocation());
        roomRepository.save(room);
    }

    public void updateRoom(Long id, Room room) {
        if (!roomRepository.existsById(id)) {
            logger.warn("Update failed: room id={} not found", id);
            throw new RuntimeException("Room not found");
        }
        room.setId(id);
        logger.info("Updating room id={} title={} price={} location={}", id, room.getTitle(), room.getPrice(), room.getLocation());
        roomRepository.save(room);
    }

    public void deleteRoom(Long id) {
        if (!roomRepository.existsById(id)) {
            logger.warn("Delete failed: room id={} not found", id);
            throw new RuntimeException("Room not found");
        }
        logger.info("Deleting room id={}", id);
        roomRepository.deleteById(id);
    }

    public List<Room> getAllRooms() {
        logger.debug("Fetching all rooms");
        return roomRepository.findAll();
    }
}